<?php
namespace app\agent\controller;

use app\common\controller\AgentBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class LoanTotal extends AgentBaseController{

    /**
     * @功能：根据用户统计收藏
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_total(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $nick = IRequest::get("nick");
        $phone = IRequest::get("phone");

        //判断是否为用户搜索
        $loan_favorite_where['lo.is_delete'] = IsDelete::No;
        if(!empty($user_id)) $loan_favorite_where['lo.user_id'] = $user_id;
        if(!empty($nick)) $loan_favorite_where['use.nick'] = array("like", "%{$nick}%");;
        if(!empty($phone)) $loan_favorite_where['use.phone'] = array("like", "%{$phone}%");

        //取得数据
        $loan_favorite_list = IDb::getInstance("loan_favorite as lo")
            ->setDbFiled("lo.user_id,count(*) as favorite_num")
            ->setDbJoin("user as use","use.user_id=lo.user_id")
            ->setDbWhere($loan_favorite_where)
            ->setDbGroup("lo.user_id")
            ->setDbOrder("favorite_num desc,lo.user_id")
            ->pag();

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("loan_favorite_list",$loan_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷款产品收藏记录
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_list(){
        //接收参数
        $user_id = IRequest::get("user_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $loan_favorite_where['is_delete'] = IsDelete::No;
        $loan_favorite_where['user_id'] = $user_id;
        $loan_favorite_list = IDb::getInstance("loan_favorite")
            ->setDbWhere($loan_favorite_where)
            ->setDbOrder("loan_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("loan_favorite_list",$loan_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：根据贷款统计收藏
     * @开发者：cxl
     * @return string
     */
    public function loan_favorite_total(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id");
        $loan_product_name = IRequest::get("loan_product_name");

        //判断是否为用户搜索
        $loan_favorite_where['cf.is_delete'] = IsDelete::No;
        $loan_favorite_where['cp.agent_id'] = get_login_agent_id();
        if (!empty($loan_product_id)) $loan_favorite_where['cf.loan_product_id'] = $loan_product_id;
        if (!empty($loan_product_name)) $loan_favorite_where['cp.loan_product_name'] = array("like", "%{$loan_product_name}%");

        //取得数据
        $loan_favorite_list = IDb::getInstance("loan_favorite as cf")
            ->setDbFiled("cf.loan_product_id,count(*) as favorite_num")
            ->setDbJoin('loan_product as cp','cp.loan_product_id = cf.loan_product_id')
            ->setDbWhere($loan_favorite_where)
            ->setDbGroup("cf.loan_product_id")
            ->setDbOrder("favorite_num desc,cf.loan_product_id")
            ->pag();
        if ($loan_favorite_list === false){
            error('获取收藏统计失败');
        }

        //页面赋值
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_product_name",$loan_product_name);
        $this->assign("loan_favorite_list",$loan_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：根据贷款产品收藏记录
     * @开发者：cxl
     * @return string
     */
    public function loan_favorite_list(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $loan_favorite_where['is_delete'] = IsDelete::No;
        $loan_favorite_where['loan_product_id'] = $loan_product_id;
        $loan_favorite_list = IDb::getInstance("loan_favorite")
            ->setDbWhere($loan_favorite_where)
            ->setDbOrder("loan_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_favorite_list",$loan_favorite_list);

        //渲染模板输出
        return view();
    }
}
